/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Br.Com.Cds.Academia.Configuracao;

import Br.Com.Cds.Academia.Aluno.AlunoBean;
import Br.Com.Cds.Academia.DAO.AcessoMySQL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;



/**
 *
 * @author Pedro
 */
public class ConfiguracaoControl {
    
    PreparedStatement pstm;
    ResultSet rs;
    
    
    String consultaconfiguracao = "SELECT * FROM CONFIGURACAO WHERE NOMEFANTASIA LIKE ?";
    String cadastraconfiguracao = "INSERT INTO CONFIGURACAO (RAZAOSOCIAL,NOMEFANTASIA,CNPJ,IE,ENDERECO,CEP,BAIRRO,CIDADE,UF,TELEFONE,FAX,EMAIL,SITE,CAMINHOLOGO,STATUSREDE,CAMINHOBANCO,NUMERO) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    String alteraconfiguracao= "UPDATE CONFIGURACAO SET RAZAOSOCIAL=?,NOMEFANTASIA=?,CNPJ=?,IE=?,ENDERECO=?,CEP=?,BAIRRO=?,CIDADE=?,UF=?,TELEFONE=?,FAX=?,EMAIL=?,SITE=?,CAMINHOLOGO=?,STATUSREDE=?,CAMINHOBANCO=?,NUMERO=?";
    String excluirconfiguracao = "DELETE FROM CONFIGURACAO WHERE NOMEFANTASIA=?";
    
    
    
     public void cadastraConfiguracao (ConfiguracaoBean configuracao){
        try {
            AcessoMySQL mysql = new AcessoMySQL();
            pstm = (PreparedStatement) mysql.conectar().prepareStatement(cadastraconfiguracao);
            pstm.setString(1,configuracao.getRazaosocial());
            pstm.setString(2, configuracao.getNomefantasia());
            pstm.setString(3, configuracao.getCnpj());
            pstm.setString(4, configuracao.getIe());
            pstm.setString(5, configuracao.getEndereco());
            pstm.setString(6, configuracao.getCep());
            pstm.setString(7, configuracao.getBairro());
            pstm.setString(8, configuracao.getCidade());
            pstm.setString(9, configuracao.getUf());
            pstm.setString(10, configuracao.getTelefone());
            pstm.setString(11, configuracao.getFax());
            pstm.setString(12, configuracao.getEmail());
            pstm.setString(13, configuracao.getSite());
            pstm.setString(14, configuracao.getCaminhologo());
            pstm.setString(15, configuracao.getStatusrede());
            pstm.setString(16, configuracao.getCaminhobanco());
            pstm.setString(17, configuracao.getNumero());
            pstm.executeUpdate();
            mysql.desconectar();
    
        } catch (SQLException e) {
            e.printStackTrace ();
            JOptionPane.showMessageDialog(null, "SQL EXCEPTION ! ERRO AO TENTAR CADASTRAR UMA CONFIGURACAO!","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE); 
        }
}


public void alterarConfiguracao (ConfiguracaoBean configuracao){
    
    try {
        AcessoMySQL mysql = new AcessoMySQL();
            pstm = (PreparedStatement) mysql.conectar().prepareStatement(alteraconfiguracao);
            pstm.setString(1,configuracao.getRazaosocial());
            pstm.setString(2, configuracao.getNomefantasia());
            pstm.setString(3, configuracao.getCnpj());
            pstm.setString(4, configuracao.getIe());
            pstm.setString(5, configuracao.getEndereco());
            pstm.setString(6, configuracao.getCep());
            pstm.setString(7, configuracao.getBairro());
            pstm.setString(8, configuracao.getCidade());
            pstm.setString(9, configuracao.getUf());
            pstm.setString(10, configuracao.getTelefone());
            pstm.setString(11, configuracao.getFax());
            pstm.setString(12, configuracao.getEmail());
            pstm.setString(13, configuracao.getSite());
            pstm.setString(14, configuracao.getCaminhologo());
            pstm.setString(15, configuracao.getStatusrede());
            pstm.setString(16, configuracao.getCaminhobanco());
            pstm.setString(17, configuracao.getNumero());
            pstm.executeUpdate();
            mysql.desconectar(); 
    } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null,"SQL EXCEPTION ! ERRO AO TENTAR ALTERAR UM ALUNO!","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
    }
}

    
  public List <ConfiguracaoBean> listarConfiguracao (String nomeconfiguracao){
         List <ConfiguracaoBean> configuracao = new ArrayList ();
         
         try {
             AcessoMySQL mysql = new AcessoMySQL();
             pstm = (PreparedStatement) mysql.conectar().prepareStatement(consultaconfiguracao);
             pstm.setString(1, nomeconfiguracao);
             rs = pstm.executeQuery();
             ConfiguracaoBean alu;
             
             while (rs.next()){
                alu = new ConfiguracaoBean ();
                alu.setRazaosocial(rs.getString("CONFIGURACAO"));          
                alu.setNomefantasia(rs.getString("NOMEFANTASIA"));  
                alu.setCnpj (rs.getString("CNPJ")); 
                alu.setIe(rs.getString("IE")); 
                alu.setEndereco(rs.getString("ENDERECO"));  
                alu.setCep(rs.getString("CEP"));   
                alu.setBairro(rs.getString("BAIRRO"));   
                alu.setCidade(rs.getString("CIDADE"));   
                alu.setUf(rs.getString("UF"));   
                alu.setTelefone(rs.getString("TELEFONE"));   
                alu.setFax(rs.getString("FAX"));   
                alu.setEmail(rs.getString("EMAIL"));   
                alu.setSite(rs.getString("SITE"));   
                alu.setCaminhologo(rs.getString("CAMINHOLOGO"));   
                alu.setStatusrede(rs.getString("STATUSREDE"));   
                alu.setCaminhobanco(rs.getString("CAMINHOBANCO"));   
                alu.setNumero(rs.getString("NUMERO"));   
                configuracao.add(alu);  
             }    
      } catch (Exception e) {
        e.printStackTrace();
         JOptionPane.showMessageDialog(null,"SQL EXCEPTION - ERRO AO TENTAR REALIZAR UM ALUNO","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
        
      }
        return configuracao;
      
  }
    
    
  public void excluirConfiguracao (String nomefantasia){
      
      try {
          AcessoMySQL mysql = new AcessoMySQL();
          pstm = (PreparedStatement) mysql.conectar().prepareStatement(excluirconfiguracao);
          pstm.setString(1,nomefantasia);
          pstm.executeUpdate();
          mysql.desconectar();
            
      } catch (Exception e) {
          e.printStackTrace();
          JOptionPane.showMessageDialog(null,"SQL EXCEPTION - ERRO AO TENTAR EXCLUIR CONFIGURACAO","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
      }
      
      
      
      
  }
    
    
    
}